package yesjava;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Main {
    public static void main(String[] args) throws Exception {
//        通过反射创建 MyObject 对象，反射方式执行 show 方法，参数值为null,取注解 defaultValue 的值
        Class<MyObject> clazz = MyObject.class;
        Constructor<MyObject> constructor = clazz.getConstructor();
        MyObject myObject = constructor.newInstance();
        Field v = clazz.getDeclaredField("v");
        v.setAccessible(true);
        v.set(myObject,15);
        Method showMethod = clazz.getDeclaredMethod("show", Integer.class);
        Integer param = null;
        if(param!=null){
            showMethod.invoke(myObject,param);
        }else{
            Parameter[] parameters = showMethod.getParameters();
            Parameter parameter = parameters[0];
            MyAnnotation annotation = parameter.getAnnotation(MyAnnotation.class);
            if(annotation==null){
                throw new RuntimeException("参数缺");
            }
            int defaultValue = annotation.defaultValue();
            showMethod.invoke(myObject,defaultValue);
        }

    }
}
